package cn.ihuizi.action;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;

import cn.ihuizi.dao.AdminDao;
import cn.ihuizi.dao.impl.AdminDaoImpl;
import cn.ihuizi.entity.Admin;
import cn.ihuizi.util.Admin_Id;

/**
 * Servlet implementation class AdminSelectIdServlet
 */
@WebServlet("/AdminSelectSelServlet")
public class AdminSelectSelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*response.getWriter().append("Served at: ").append(request.getContextPath());*/
		AdminDao adminDao = new AdminDaoImpl();
		String account = Admin_Id.account;
		ResultSet rs = adminDao.selectAdminSel(new Admin(account));
		ResultSet rs1 = adminDao.selectAdminRole(new Admin(account));
		List<String> list = new ArrayList<>();
		try {
			while (rs.next()) {
				request.setAttribute("name", rs.getString(1));	
				request.setAttribute("phone",rs.getString(2));	
				request.setAttribute("email", rs.getString(3));	
				request.setAttribute("time", rs.getString(4));	
				
				
			}
			while (rs1.next()) {
				String role = rs1.getString(1);
				Vector<String> vc = new Vector();
				vc.add(role);
				StringBuffer string = new StringBuffer();
				for(int i = 0; i<vc.size(); i++) {
					if(string.length() > 0) {
						string.append(",");
					}
					string.append(vc.get(i));
				}
				list.add(string.toString());
				StringBuffer string2 = new StringBuffer();
				for(int i = 0; i<list.size(); i++) {
					if(string2.length() > 0) {
						string2.append(",");
					}
					string2.append(list.get(i));
				}
				request.setAttribute("role",string2);	
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("view/user/user_info.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
